package com.gdpu.watermelontimer

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.gdpu.watermelontimer.data.AppDatabase
import com.gdpu.watermelontimer.data.User
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        val etUsername = findViewById<EditText>(R.id.et_username)
        val etPassword = findViewById<EditText>(R.id.et_password)
        val btnLogin = findViewById<Button>(R.id.btn_login)
        val btnRegister = findViewById<Button>(R.id.btn_register)
        val db = AppDatabase.getDatabase(this)
        val userDao = db.userDao()

        btnRegister.setOnClickListener {
            val username = etUsername.text.toString().trim()
            val password = etPassword.text.toString().trim()
            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            CoroutineScope(Dispatchers.IO).launch {
                val exist = userDao.getUserByUsername(username)
                if (exist != null) {
                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@LoginActivity, "账号已存在", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    val user = User(username = username, password = password)
                    val userId = userDao.insertUser(user)
                    withContext(Dispatchers.Main) {
                        saveLoginUserId(userId)
                        Toast.makeText(this@LoginActivity, "注册成功，已自动登录", Toast.LENGTH_SHORT).show()
                        goToMain()
                    }
                }
            }
        }

        btnLogin.setOnClickListener {
            val username = etUsername.text.toString().trim()
            val password = etPassword.text.toString().trim()
            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            CoroutineScope(Dispatchers.IO).launch {
                val user = userDao.login(username, password)
                withContext(Dispatchers.Main) {
                    if (user != null) {
                        saveLoginUserId(user.id)
                        Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
                        goToMain()
                    } else {
                        Toast.makeText(this@LoginActivity, "账号或密码错误", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

    private fun saveLoginUserId(userId: Long) {
        getSharedPreferences("user", Context.MODE_PRIVATE)
            .edit().putLong("userId", userId).apply()
    }

    private fun goToMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
} 